﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Security.Principal;
using System.Threading;

namespace RevClinic.Core.Security
{
    public class RevClinicPrincipal : GenericPrincipal
    {
        public readonly ReadOnlyCollection<string> Roles;

        public RevClinicPrincipal(RevClinicIdentity identity, string[] roles) :
            base(identity, roles)
        {
            Roles = new ReadOnlyCollection<string>(roles ?? new string[0]);
        }

        public override bool IsInRole(string role)
        {
            bool isInRole = base.IsInRole(role);
            if (isInRole == false) {
                isInRole = Roles.Any(r => role.StartsWith(r));
            }
            return isInRole;
        }

        public static RevClinicPrincipal Current
        {
            get { return Thread.CurrentPrincipal as RevClinicPrincipal; }
        }

    }
}
